/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template  
 */
package com.apache.design.template;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.design.template.helper.*;
import com.apache.design.util.DatabaseUtils;
import com.apache.design.util.DesignEntity;
import com.apache.info.entity.PubSortInfo;
import com.apache.rpc.common.LoadRpcService;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description: 生成sql  
 */
public class BuildProjectFactory {

	private Logger log = Logger.getLogger(getClass());
	private static BuildProjectFactory instance = new BuildProjectFactory();
	private Map<String, TemplateManager> temps = new HashMap<String, TemplateManager>();

	private BuildProjectFactory() {
		temps.put("daoTemp", new DaoTempManager());
		temps.put("entityTemp", new EntityTempManager());
		temps.put("managerTemp",new ManagerTempManager());
		temps.put("ibatisTemp", new IbatisTempManager());
		temps.put("springTemp", new SpringTempManager());
		temps.put("projectTemp", new ProjectTempManager());
		temps.put("actionTemp", new ControllerTempManager());
		temps.put("configTemp", new SystemConfigTempManager());
	}

	public static BuildProjectFactory instance() {
		return instance;
	}

	@SuppressWarnings("rawtypes")
	public boolean ctreateProject(Map param) {
		boolean mark = false;
		DesignEntity deVo = new DesignEntity();
		deVo.setParam(param);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysName", param.get("sysEname"));
		map.put("method", "list");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = LoadRpcService.service().doService("infoService", "sortInfoAction", "xml", map, null);
		if(!Validator.isEmpty(entity.getEntity())) {
			List list = (List) entity.getEntity();
			deVo.setTabs(list);
		} else {
			List tables = buildParam(param);
			deVo.setTabs(tables);
		}
		deVo.setSysEname(deVo.getParam("sysEname"));
		deVo.setFilePath(deVo.getParam("filePath"));
		String packPath = Validator.getDefaultStr(deVo.getParam("packPath"),"com.apache."+deVo.getSysEname());
		deVo.setPackPath(packPath);
		deVo.setProjectName(deVo.getParam("projectName"));
		deVo.setCreateAction(false);
			//全部
		for(String key : temps.keySet()){
			mark = temps.get(key).doInvoke(deVo);
		}
		Connection conn = getConn(param);
		DatabaseUtils.getInstance().close(conn,null);

		return mark;
	}

	/**
	 * 生成实体
	 */
	public boolean ctreateEntity(Map param) {
		boolean mark = false;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysName",param.get("sysName"));
		map.put("method","list");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = LoadRpcService.service().doService("infoService","sortInfoAction","xml",map,null);
		if (StrUtil.isEmpty(entity.getEntity())) {
			log.info("没有获取到系统信息");
			return mark;
		}
		List list = (List) entity.getEntity();
		if (StrUtil.isEmpty(list)) {
			log.info("没有获取到数据表信息");
			return mark;
		}
		DesignEntity deVo = new DesignEntity();
		param.put("sortList", list);
		mark = temps.get("entityTemp").doInvoke(deVo);
		return mark;
	}

	/**
	 * 生成action
	 */
	public boolean ctreateAction(Map param) {
		boolean mark = false;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysName",param.get("sysName"));
		map.put("method","list");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = LoadRpcService.service().doService("infoService","sortInfoAction","xml",map,null);
		if (StrUtil.isEmpty(entity.getEntity())) {
			log.info("没有获取到系统信息");
			return mark;
		}
		List list = (List) entity.getEntity();
		if (StrUtil.isEmpty(list)) {
			log.info("没有获取到数据表信息");
			return mark;
		}
		DesignEntity deVo = new DesignEntity();
		param.put("sortList", list);
		deVo.setCreateAction(false);
		mark = temps.get("actionTemp").doInvoke(deVo);
		return mark;
	}

	/**
	 * 生成Manager
	 */
	public boolean ctreateManager(Map param) {
		boolean mark = false;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysName",param.get("sysName"));
		map.put("method","list");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = LoadRpcService.service().doService("infoService","sortInfoAction","xml",map,null);
		if (StrUtil.isEmpty(entity.getEntity())) {
			log.info("没有获取到系统信息");
			return mark;
		}
		List list = (List) entity.getEntity();
		if (StrUtil.isEmpty(list)) {
			log.info("没有获取到数据表信息");
			return mark;
		}
		DesignEntity deVo = new DesignEntity();
		param.put("sortList", list);
		mark = temps.get("managerTemp").doInvoke(deVo);
		return mark;
	}

	/**
	 * 生成mybatis配置
	 */
	public boolean ctreateMybtaits(Map param) {
		boolean mark = false;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysName",param.get("sysName"));
		map.put("method","list");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = LoadRpcService.service().doService("infoService","sortInfoAction","xml",map,null);
		if (StrUtil.isEmpty(entity.getEntity())) {
			log.info("没有获取到系统信息");
			return mark;
		}
		List list = (List) entity.getEntity();
		if (StrUtil.isEmpty(list)) {
			log.info("没有获取到数据表信息");
			return mark;
		}
		DesignEntity deVo = new DesignEntity();
		param.put("sortList", list);
		mark = temps.get("ibatisTemp").doInvoke(deVo);
		return mark;
	}

	/**
	 * 生成spring配置
	 */
	public boolean ctreateSpring(Map param) {
		boolean mark = false;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysName",param.get("sysName"));
		map.put("method","list");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = LoadRpcService.service().doService("infoService","sortInfoAction","xml",map,null);
		if (StrUtil.isEmpty(entity.getEntity())) {
			log.info("没有获取到系统信息");
			return mark;
		}
		List list = (List) entity.getEntity();
		if (StrUtil.isEmpty(list)) {
			log.info("没有获取到数据表信息");
			return mark;
		}
		DesignEntity deVo = new DesignEntity();
		param.put("sortList", list);
		mark = temps.get("springTemp").doInvoke(deVo);
		return mark;
	}

	/**
	 * 生成系统配置
	 */
	public boolean ctreateConfig(Map param) {
		boolean mark = false;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sysName",param.get("sysName"));
		map.put("method","list");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = LoadRpcService.service().doService("infoService","sortInfoAction","xml",map,null);
		if (StrUtil.isEmpty(entity.getEntity())) {
			log.info("没有获取到系统信息");
			return mark;
		}
		List list = (List) entity.getEntity();
		if (StrUtil.isEmpty(list)) {
			log.info("没有获取到数据表信息");
			return mark;
		}
		DesignEntity deVo = new DesignEntity();
		param.put("sortList", list);
		mark = temps.get("configTemp").doInvoke(deVo);
		return mark;
	}

	/**
	 * 生成Dao配置
	 */
	public boolean ctreateDao(Map param) {
		boolean mark = false;
		Map<String, Object> map = new HashMap<String, Object>();
		List tables = buildParam(param);

		map.put("sysName",param.get("sysEname"));
		map.put("method","list");
		map.put("sysPass", ConfigUtil.getInstance().interfacePass());
		ResultEntity entity = LoadRpcService.service().doService("infoService","sortInfoAction","xml",map,null);
		if (StrUtil.isEmpty(entity.getEntity())) {
			log.info("没有获取到系统信息");
			return mark;
		}
		List list = (List) entity.getEntity();
		if (StrUtil.isEmpty(list)) {
			log.info("没有获取到数据表信息");
			return mark;
		}
		DesignEntity deVo = new DesignEntity();
		param.put("sortList", list);
		mark = temps.get("daoTemp").doInvoke(deVo);
		return mark;
	}

	private List<PubSortInfo> buildParam(Map<String,String> param){
		String sysEname = param.get("sysEname");
		Connection conn = getConn(param);
		List<PubSortInfo> list = DatabaseUtils.getInstance().getAllTableNamesMap(conn,sysEname);
		DatabaseUtils.getInstance().close(conn,null);
		return list;
	}

	private Connection getConn(Map<String,String> param){
		String jdbcUrl = param.get("jdbcUrl");
		String driverClassName = param.get("driverClassName");
		String user = param.get("jdbcUsername");
		String pwd = param.get("jdbcPassword");
		String sysEname = param.get("sysEname");
		Connection conn = DatabaseUtils.getInstance().getConnection(sysEname,jdbcUrl,driverClassName,user,pwd);
		return conn;
	}
}
